<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/19
 * Time: 15:58
 */

namespace common\logics\taskorder;

use common\widgets\Tool;
use Yii;
use yii\base\BaseObject;
use yii\db\Exception;

class TaskOrderLogic extends BaseObject implements ITaskOrder
{
    /** @var \common\sources\read\taskorder\TaskOrderAccess  $RTaskOrderAccess  ***/
    private $RTaskOrderAccess;
    /** @var \common\sources\write\taskorder\TaskOrderAccess $WTaskOrderAccess ***/
    private $WTaskOrderAccess;
    /** @var \common\sources\read\buyuser\BuyUserAccess  $RBuyUserAccess ***/
    private $RBuyUserAccess;
    /*** @var \common\sources\read\taskcommod\TaskCommodAccess $RTaskCommodAccess **/
    private $RTaskCommodAccess;
    /** @var \common\sources\write\taskcommod\TaskCommodAccess  $WTaskCommodAccess **/
    private $WTaskCommodAccess;
    /** @var \common\sources\read\taskshopimage\TaskShopImageAccess  $RTaskShopImageAccess ***/
    private $RTaskShopImageAccess;
    /** @var \common\sources\write\taskshopimage\TaskShopImageAccess  $WTaskShopImageAccess **/
    private $WTaskShopImageAccess;
    /** @var \common\sources\read\valueservice\ValueServiceAccess $RValueServiceAccess **/
    private $RValueServiceAccess;
    /** @var \common\sources\read\buyuser\BuyUserAcountAccess $RBuyUserAcountAccess ***/
    private $RBuyUserAcountAccess;
    /** @var \common\sources\write\depositrecord\DepositRecordAccess  $WDepositRecordAccess ***/
    private $WDepositRecordAccess;
    /** @var \common\sources\read\depositrecord\DepositRecordAccess  $RDepositRecordAccess **/
    private $RDepositRecordAccess;
    /** @var \common\sources\write\pepperrecord\PepperRecordAccess $WPepperRecordAccess ****/
    private $WPepperRecordAccess;
    /** @var \common\sources\read\pepperrecord\PepperRecordAccess  $RPepperRecordAccess **/
    private $RPepperRecordAccess;
    /** @var \common\sources\write\buyuser\BuyUserAcountAccess  $WBuyUserAcountAccess **/
    private $WBuyUserAcountAccess;
    /** @var \common\sources\read\busdepositrecord\BusDepositRecordAccess  $RBusDepositRecordAccess ***/
    private $RBusDepositRecordAccess;
    /** @var \common\sources\write\busdepositrecord\BusDepositRecordAccess $WBusDepositRecordAccess **/
    private $WBusDepositRecordAccess;
    /** @var \common\sources\read\businessaccount\BusinessAccountAccess $RBusinessAccountAccess **/
    private $RBusinessAccountAccess;
    /** @var \common\sources\write\businessaccount\BusinessAccountAccess  $WBusinessAccountAccess ***/
    private $WBusinessAccountAccess;
    /** @var \common\sources\write\taskpepperrecord\TaskPepperRecordAccess $WTaskPepperRecordAccess ***/
    private $WTaskPepperRecordAccess;
    /** @var \common\sources\write\taskorderlog\BuyTaskOrderLogAccess $WBuyTaskOrderLogAccess ***/
    private $WBuyTaskOrderLogAccess;
    /** @var \common\sources\write\taskcommodkeywords\TaskCommodKeywordAccess  $WTaskCommodKeywordAccess  **/
    private $WTaskCommodKeywordAccess;
    /** @var \common\sources\read\taskcommodkeywords\TaskCommodKeywordAccess  $RTaskCommodKeywordAccess  **/
    private $RTaskCommodKeywordAccess;
    /** @var \common\sources\read\praisekeywords\PraiseKeywordsAccess $RPraiseKeywordsAccess  **/
    private $RPraiseKeywordsAccess;
    /** @var \common\sources\write\praisekeywords\PraiseKeywordsAccess $WPraiseKeywordsAccess  **/
    private $WPraiseKeywordsAccess;
    /** @var \common\sources\write\buspepperrecord\BusPepperRecordAccess $WBusPepperRecordAccess **/
    private $WBusPepperRecordAccess;
    /** @var \common\sources\read\shop\ShopAccess $RShopAccess **/
    private $RShopAccess;

    public function init()
    {
        $this->RTaskOrderAccess = Yii::$container->get("RTaskOrderAccess");
        $this->WTaskOrderAccess = Yii::$container->get("WTaskOrderAccess");
        $this->RTaskCommodAccess = Yii::$container->get("RTaskCommodAccess");
        $this->WTaskCommodAccess = Yii::$container->get("WTaskCommodAccess");
        $this->RBuyUserAccess = Yii::$container->get("RBuyUserAccess");
        $this->RValueServiceAccess = Yii::$container->get("RValueServiceAccess");
        $this->RTaskShopImageAccess = Yii::$container->get("RTaskShopImageAccess");
        $this->WTaskShopImageAccess = Yii::$container->get("WTaskShopImageAccess");
        $this->RBuyUserAcountAccess = Yii::$container->get("RBuyUserAcountAccess");
        $this->WDepositRecordAccess = Yii::$container->get("WDepositRecordAccess");
        $this->RDepositRecordAccess = Yii::$container->get("RDepositRecordAccess");
        $this->WPepperRecordAccess = Yii::$container->get("WPepperRecordAccess");
        $this->RPepperRecordAccess = Yii::$container->get("RPepperRecordAccess");
        $this->WBuyUserAcountAccess = Yii::$container->get("WBuyUserAcountAccess");
        $this->RBusDepositRecordAccess = Yii::$container->get("RBusDepositRecordAccess");
        $this->WBusDepositRecordAccess = Yii::$container->get("WBusDepositRecordAccess");
        $this->RBusinessAccountAccess = Yii::$container->get("RBusinessAccountAccess");
        $this->WBusinessAccountAccess = Yii::$container->get("WBusinessAccountAccess");
        $this->WTaskPepperRecordAccess = Yii::$container->get("WTaskPepperRecordAccess");
        $this->WBuyTaskOrderLogAccess = Yii::$container->get("WBuyTaskOrderLogAccess");
        $this->WTaskCommodKeywordAccess = Yii::$container->get("WTaskCommodKeywordAccess");
        $this->RTaskCommodKeywordAccess = Yii::$container->get("RTaskCommodKeywordAccess");
        $this->RPraiseKeywordsAccess = Yii::$container->get("RPraiseKeywordsAccess");
        $this->WPraiseKeywordsAccess = Yii::$container->get("WPraiseKeywordsAccess");
        $this->WBusPepperRecordAccess = Yii::$container->get("WBusPepperRecordAccess");
        $this->RShopAccess = Yii::$container->get("RShopAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }

    //任务订单列表
    public function getTaskOrderList($data)
    {
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],401,"买手id不能为空");
        }
        $page_num = $data['page_num'];
        $count = $this->RTaskOrderAccess->getTaskOrderCount($data);
        $list = [];
        $all_count = $this->RTaskOrderAccess->getTaskOrderAllCount($data);
        $menu['all_count'] = $all_count;
        $menu['received_count'] = 0;
        $menu['good_stay'] = 0;
        $menu['return_money'] = 0;
        $menu['confirm'] = 0;
        $menu['reject'] = 0;
        $menu['complete'] = 0;

        if($count >0)
        {
            $list = $this->RTaskOrderAccess->getTaskOrderAll($data);

            foreach ($list as &$item)
            {
                $item['order_status_name'] = isset($item['order_status']) ? (isset(Yii::$app->params['task_order_status_name'][$item['order_status']]) ? Yii::$app->params['task_order_status_name'][$item['order_status']] : "未知状态") : "";
                $item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s',$item['create_time']): '';
                $item['plat_type_name'] = isset($item['plat_type']) ? Yii::$app->params['plat_type'][$item['plat_type']] : '';
                $task_types = Yii::$app->params['plat_task_type'][$item['plat_type']];
                if(count($task_types) >0)
                {
                    foreach ($task_types as $task_ty)
                    {
                        if($task_ty['task_type'] == $item['task_type'])
                        {
                            $item['task_type_name'] = isset($item['task_type']) ? $task_ty['name'] : '';
                        }
                    }
                }
            }
        }
        //统计 已接手状态
        $received_count = $this->RTaskOrderAccess->getTaskOrderCount(['buy_id' => $data['buy_id'],'order_status' => 1]);
        //待收货
        $good_stay = $this->RTaskOrderAccess->getTaskOrderCount(['buy_id' => $data['buy_id'],'order_status' => 5]);
        //待返款
        $return_money = $this->RTaskOrderAccess->getTaskOrderCount(['buy_id' => $data['buy_id'],'order_status' => 6]);
        //待确认
        $confirm = $this->RTaskOrderAccess->getTaskOrderCount(['buy_id' => $data['buy_id'],'order_status' => 7]);
        //待驳回
        $reject = $this->RTaskOrderAccess->getTaskOrderCount(['buy_id' => $data['buy_id'],'order_status' => 9]);
        //已完成
        $complete = $this->RTaskOrderAccess->getTaskOrderCount(['buy_id' => $data['buy_id'],'order_status' => 8]);
        $menu['received_count'] = $received_count;
        $menu['good_stay'] = $good_stay;
        $menu['return_money'] = $return_money;
        $menu['confirm'] = $confirm;
        $menu['reject'] = $reject;
        $menu['complete'] = $complete;
        return Tool::returnJson([
            "list" => $list,
            "count" => $count,
            "menues" => $menu,
            "limit" => $data['limit'],
            "page_num" => $page_num
        ],200,"成功");
    }
    //任务订单详情
    public function getTaskOrder($data)
    {
        header("content-type:text/html;charset=utf-8");
        if(empty($data['task_order_num']))
        {
            return Tool::returnJson([],401,"子订单不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }

        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],403,"买手账号不存在");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderDetail($data['task_order_num']);
        if(empty($task_order))
        {
            return Tool::returnJson([],405,"子任务单号不存在");
        }
        if($task_order['buy_id'] != $data['buy_id'])
        {
            return Tool::returnJson([],406,"任务对应的子任务不正确");
        }
        //判断子任务是否是图文任务
        if(($task_order['plat_type'] == 1 || $task_order['plat_type'] == 2) && $task_order['task_type'] ==2)
        {
           $task_order['task_shop_image']  = $this->RTaskShopImageAccess->getTaskShopDetail($task_order['task_shop_image_id']);
           if(!empty($task_order['task_shop_image']))
            {
                    $slide_imgs = [];
                    if(!empty( $task_order['task_shop_image']['slide_img1']))
                    {
                        array_push($slide_imgs, $task_order['task_shop_image']['slide_img1']);
                    }
                    if(!empty( $task_order['task_shop_image']['slide_img2']))
                    {
                        array_push($slide_imgs, $task_order['task_shop_image']['slide_img2']);
                    }
                    if(!empty( $task_order['task_shop_image']['slide_img3']))
                    {
                        array_push($slide_imgs, $task_order['task_shop_image']['slide_img3']);
                    }
                    if(!empty( $task_order['task_shop_image']['slide_img4']))
                    {
                        array_push($slide_imgs, $task_order['task_shop_image']['slide_img4']);
                    }
                    if(!empty( $task_order['task_shop_image']['slide_img5']))
                    {
                        array_push($slide_imgs, $task_order['task_shop_image']['slide_img5']);
                    }
                    $task_order['task_shop_image']['slide_imgs'] = $slide_imgs;
            }

        }
        //获取增值服务详情
        $value_service = $this->RValueServiceAccess->getValueServiceByCommodId($task_order['task_commod_id']);
        if(!empty($value_service['praise']) && isset($value_service['praise']))
        {
            if($value_service['praise'] == 2)
            {
                $task_order['praise_keys'] = str_replace( "|","  ",$value_service['praise_keys']);
            }
            $task_order['chat_type'] = $value_service['chat_type'];
            $task_order['chat_keys'] = $value_service['chat_keys'];
        }
        //标题
        $title = trim($task_order['title']);
        $task_order['title'] = $this->func_substr_replace($title,"*",2,mb_strlen($title,"utf-8")-4);
        //旺旺名称
        $ww_name = trim($task_order['ww_name']);
        $task_order['ww_name'] = $this->func_substr_replace($ww_name,"*",1,mb_strlen($ww_name,"utf-8")-2);
        //店铺名称
        $shop_name = trim($task_order['shop_name']);
        $task_order['shop_name'] = $this->func_substr_replace($shop_name,"*",2,mb_strlen($shop_name,"utf-8")-4);

        $task_order['search_time'] = isset($task_order['search_time']) ? date('Y-m-d H:i:s',$task_order['search_time']) : "---";
        $task_order['browse_time'] = isset($task_order['browse_time']) ? date('Y-m-d H:i:s',$task_order['browse_time']) : "--";
        $task_order['order_time'] = isset($task_order['order_time']) ? date('Y-m-d H:i:s',$task_order['order_time']) : "--";
        $task_order['cancel_time'] = isset($task_order['cancel_time']) ? date('Y-m-d H:i:s',$task_order['cancel_time']) : "--";
        $task_order['merchant_time'] = isset($task_order['merchant_time']) ? date('Y-m-d H:i:s',$task_order['merchant_time']) : "--";
        $task_order['favorable_time'] = isset($task_order['favorable_time']) ? date('Y-m-d H:i:s',$task_order['favorable_time']) : "--";
        $task_order['plat_return_time'] = isset($task_order['plat_return_time']) ? date('Y-m-d H:i:s',$task_order['plat_return_time']) : "--";
        $task_order['confirm_time'] = isset($task_order['confirm_time']) ? date('Y-m-d H:i:s',$task_order['confirm_time']) : "--";
        $task_order['give_up_time'] = isset($task_order['give_up_time']) ? date('Y-m-d H:i:s',$task_order['give_up_time']) : "--";
        $task_order['reject_time'] = isset($task_order['reject_time']) ? date('Y-m-d H:i:s',$task_order['reject_time']) : "--";
        $task_order['create_time'] = isset($task_order['create_time']) ? date('Y-m-d H:i:s',$task_order['create_time']) : "--";

        $task_types = Yii::$app->params['plat_task_type'][$task_order['plat_type']];
        foreach ($task_types as &$task_ty)
        {
            if($task_ty['task_type'] == $task_order['task_type'])
            {
                $task_order['task_type_name'] = isset($task_order['task_type']) ? $task_ty['name'] : "";
            }

        }
        return Tool::returnJson($task_order,200,"成功");
    }
    /**
     * 隐藏标题
     *  $replacement 替换符号
     *  $start 隐藏开始
     *  $sublen  隐藏长度
    **/
    private function func_substr_replace($str, $replacement = '*', $start = 1, $length = 3)
    {
        $len = mb_strlen($str, 'utf-8');
        if ($len > intval($start + $length)) {
            $str1 = mb_substr($str, 0, $start, 'utf-8');
            $str2 = mb_substr($str, intval($start + $length), NULL, 'utf-8');
        } else {
            $str1 = mb_substr($str, 0, 1, 'utf-8');
            $str2 = mb_substr($str, $len - 1, 1, 'utf-8');
            $length = $len - 2;
        }
        $new_str = $str1;
//        for ($i = 0; $i < $length; $i++) {
//            $new_str .= $replacement;
//        }
        $new_str .= $replacement.$replacement.$replacement.$replacement.$replacement.$replacement;
        $new_str .= $str2;
        return $new_str;
    }
    //浏览子任务 第三步
    public function browseTask($data)
    {
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],401,"子订单id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }
        if(empty($data['bb_url']))
        {
            return Tool::returnJson([],403,"宝贝链接不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],405,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],406,"安全码不正确");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],407,"买手账号不存在");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],408,"子任务单号不存在");
        }
//        if($task_order['task_shop_status'] !=3)
//        {
//            return Tool::returnJson([],409,"该子订单不能浏览");
//        }
        if($task_order['is_check'] !=1)
        {
            return Tool::returnJson([],409,"子订单链接尚未核对");
        }
        $task_commod = $this->RTaskCommodAccess->getTaskDetail($task_order['task_commod_id']);
        if(empty($task_commod))
        {
            return Tool::returnJson([],410,"主订单不存在");
        }


        //核对链接
        $commod_arr = explode('?',$data['bb_url']);
        if(count($commod_arr) <2)
        {
            return Tool::returnJson([],410,"链接有误,请重新输入");
        }
        $item_arr = explode("&",$commod_arr[1]);
        $item_url_arr = [];
        if(count($item_arr) >0)
        {
            foreach ($item_arr as $item)
            {
                $arr = explode("=",$item);
                if(count($arr) >1)
                {
                    $item_url_arr[$arr[0]] = $arr[1];
                }
            }
            $item_url_arr['http_url'] = $commod_arr[0];
        }
        $task_commod_arr = explode('?',$task_commod['commod_link']);
        if(count($task_commod_arr)<2)
        {
            return Tool::returnJson([],411,"商品链接有误,请重新输入");
        }
        $task_comm_item_arr = explode("&",$task_commod_arr[1]);
        $task_commd_url_arr = [];
        if(count($task_comm_item_arr) >0)
        {
            foreach ($task_comm_item_arr as $task_item)
            {
                $task_arr = explode("=",$task_item);
                if(count($task_arr) >1)
                {
                    $task_commd_url_arr[$task_arr[0]] = $task_arr[1];
                }
            }
            $task_commd_url_arr['http_url'] = $task_commod_arr[0];
        }
        //
        if(!empty($task_commd_url_arr) && !empty($item_url_arr))
        {
            //核对链接
            if($task_commd_url_arr['id'] != $item_url_arr['id'])
            {
                return Tool::returnJson([],412,"链接不匹配");
            }
        }else
        {
            return Tool::returnJson([],413,"商品链接或者核对链接有误");
        }

        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->browseTask(["order_status" => 1,"id" =>$data['task_order_id'],"task_shop_status"=>4 ]);
            if($res)
            {
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],406,"浏览子任务失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],500,"操作异常");
        }
    }
    //完成任务第一步
    public function checkLink($data)
    {
        if(empty($data['bb_url']))
        {
            return Tool::returnJson([],401,"宝贝链接不能为空");
        }
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],402,"任务id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],403,"买手id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],405,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],406,"安全码不正确");
        }
        //判断任务是否存在
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],407,"子任务订单不存在");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],408,"买手账号不存在");
        }
        //判断主任务订单情况
        $task_commod = $this->RTaskCommodAccess->getTaskDetail($task_order['task_commod_id']);
        if(empty($task_commod))
        {
            return Tool::returnJson([],409,"主任务不存在");
        }

        $commod_arr = explode('?',$data['bb_url']);
        if(count($commod_arr) <2)
        {
            return Tool::returnJson([],410,"链接有误,请重新输入");
        }
        $item_arr = explode("&",$commod_arr[1]);
        $item_url_arr = [];
        if(count($item_arr) >0)
        {
            foreach ($item_arr as $item)
            {
                $arr = explode("=",$item);
                if(count($arr) >1)
                {
                    $item_url_arr[$arr[0]] = $arr[1];
                }
            }
            $item_url_arr['http_url'] = $commod_arr[0];
        }
        $task_commod_arr = explode('?',$task_commod['commod_link']);
        if(count($task_commod_arr)<2)
        {
            return Tool::returnJson([],411,"商品链接有误,请重新输入");
        }
        $task_comm_item_arr = explode("&",$task_commod_arr[1]);
        $task_commd_url_arr = [];
        if(count($task_comm_item_arr) >0)
        {
            foreach ($task_comm_item_arr as $task_item)
            {
                $task_arr = explode("=",$task_item);
                if(count($task_arr) >1)
                {
                    $task_commd_url_arr[$task_arr[0]] = $task_arr[1];
                }
            }
            $task_commd_url_arr['http_url'] = $task_commod_arr[0];
        }
        //
        if(!empty($task_commd_url_arr) && !empty($item_url_arr))
        {
            //核对链接
            if($task_commd_url_arr['id'] != $item_url_arr['id'])
            {
                return Tool::returnJson([],412,"链接不匹配");
            }
        }else
        {
            return Tool::returnJson([],413,"商品链接或者核对链接有误");
        }

        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->checkUrl($data['task_order_id']);
            if($res)
            {
                $transaction->commit();
                return Tool::returnJson([],200,"核对链接成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],415,"核对链接失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"核对链接异常");
        }
    }
    public function chekLinkNext($data)
    {
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],401,"任务id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        //判断任务是否存在
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],406,"子任务订单不存在");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],407,"买手账号不存在");
        }
        if($task_order['buy_id'] != $data['buy_id'])
        {
            return Tool::returnJson([],408,"非买手本人任务不能进行该操作");
        }
//        if(!($task_order['is_check'] == 1 && $task_order['task_shop_status'] ==1) )
//        {
//            return Tool::returnJson([],409,"该状态不能下一步");
//        }
        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->modifyTaskOrder(['id'=> $data['task_order_id'],'task_shop_status'=>2]);
            if($res)
            {
                $transaction->commit();
                return Tool::returnJson([],200,"操作成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],410,"操作失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"操作异常".$e->getMessage());
        }
    }

    //第二步  上传关键子图片
    public function uploadImg($data)
    {
        if(empty($data['keywords_url']))
        {
            return Tool::returnJson([],401,"上传关键字截图不能为空");
        }
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],402,"任务id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],403,"买手id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],405,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],406,"安全码不正确");
        }
        //判断任务是否存在
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],407,"子任务订单不存在");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],408,"买手账号不存在");
        }
        if($task_order['buy_id'] != $data['buy_id'])
        {
            return Tool::returnJson([],409,"买手对应子任务不存在");
        }
//        if($task_order['task_shop_status'] != 2)
//        {
//            return Tool::returnJson([],410,"该状态下不能上传关键子截图");
//        }
        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->uploadKeywordImg(['id' => $data['task_order_id'],'keyword_url'=> $data['keywords_url'],"task_shop_status"=>3]);
            if($res)
            {
                $transaction->commit();
                return Tool::returnJson([],200,"上传成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],411,"操作失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"操作异常");
        }
    }

    //提交订单
    public function submitTask($data)
    {
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],401,"子订单id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        if(empty($data['actual_price']))
        {
            return Tool::returnJson([],406,"实际付款金额不能为空");
        }
        if(empty($data['purchase_num']))
        {
            return Tool::returnJson([],407,"购买件数不能为空");
        }
        if(empty($data['pay_order_num']))
        {
            return Tool::returnJson([],408,"付款订单号不能为空");
        }
        if(empty($data['addr_url']))
        {
            return Tool::returnJson([],409,"地址截图地址不能为空");
        }
        if(empty($data['pay_url']))
        {
            return Tool::returnJson([],410,"付款金额截图不能为空");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],411,"买手账号不存在");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],412,"子任务单号不存在");
        }
        //查询订单号是否已经存在
        $shop_order_num_count = $this->RTaskOrderAccess->getTaskOrderByNum($data['pay_order_num']);
        if($shop_order_num_count >0)
        {
            return Tool::returnJson([],413,"淘宝任务单号已经提交,不能提交重复订单号");
        }
        if($task_order['task_shop_status'] != 4)
        {
            return Tool::returnJson([],415,"该商品订单任务不能提交");
        }
        if($data['actual_price'] <=0)
        {
            return Tool::returnJson([],416,"时间付款金额不能小于0");
        }
        //判断订单情况
        if(abs($task_order['price']* $task_order['num'] - $data['actual_price'])>20)
        {
            return Tool::returnJson([],417,"实际付款金额与原商品价格差别太大");
        }
        if($task_order['num'] != $data['purchase_num'])
        {
            return Tool::returnJson([],418,"购买件数与任务每单件数不一致");
        }

        if($task_order['order_status'] !=1)
        {
            return Tool::returnJson([],419,"该子订单不提交");
        }
        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->submitTask(["order_status" => 2,"id" =>$data['task_order_id'],"actual_price"=> $data['actual_price'],"purchase_num" => $data['purchase_num'],
                "advance_amount"=>$data['actual_price'],"actual_refunds" => $data['actual_price'],"refund_amount" => $data['actual_price'],
                "pay_order_num" => $data['pay_order_num'],"addr_url" => $data['addr_url'],"pay_url" => $data['pay_url'],"task_shop_status"=> 5]);
            if($res)
            {
                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $data['buy_id'],
                    "task_status" => 2,
                    "status_remark" => "已下单待打印快递单号",
                    "remark" => '已付款',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyer['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],425,"添加买手操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],420,"提交任务失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"操作异常");
        }
    }

    //放弃任务
    public function geiveUpTask($data)
    {
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],401,"子订单id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }

        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],406,"买手账号不存在");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],407,"子任务单号不存在");
        }
        if($task_order['order_status'] != 1)
        {
            return Tool::returnJson([],408,"该订单不能放弃");
        }
        //查询主任务
        $taskCommod = $this->RTaskCommodAccess->getTaskDetail($task_order['task_commod_id']);
        if(empty($taskCommod))
        {
            return Tool::returnJson([],410,"主任务不存在,暂时不能取消");
        }
        //放弃任务次数限制
        $give_up_count = $this->RTaskOrderAccess->getBuyTaskOrderDayCount($data['buy_id'],10,strtotime(date('Y-m-d',time())),strtotime(date('Y-m-d',time())." 23:59:59"));
        if($give_up_count >2)
        {
            return Tool::returnJson([],411,"你放弃任务已经超过3次");
        }

        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->modifyTask(["order_status" => 10,"id" =>$data['task_order_id'] ]);
            if($res)
            {
                //返回商家 任务个数
                if($taskCommod['task_status'] == 5)
                {

                    $value_service = $this->RValueServiceAccess->getValueServiceByCommodId($taskCommod['id']);
                    if(empty($value_service))
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],412,"商家增值服务异常");
                    }
                    $res_task = $this->WTaskCommodAccess->modifyTaskCommod(["task_answer" => $taskCommod['task_answer']-1,"surplus_task_num" => $taskCommod['surplus_task_num']+1,"id"=>$task_order['task_commod_id']]);
                    if(!$res_task)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],413,"取消任务失败");
                    }
                    $businessAccount = $this->RBusinessAccountAccess->getBusinessAccount($taskCommod['bus_id']);
                    if(empty($businessAccount))
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],415,"商家账号异常");
                    }
                    //修改账号佣金
                    $service_pepper = number_format($value_service['return_type_pepper']+ $value_service['chat_pepper'] + $value_service['extend_pepper'] + $value_service['sex_pepper']
                        + $value_service['praise_pepper'] + $value_service['reword_commise'] + $value_service['service_pepper'] + $taskCommod['item_commisse'],2,".","");
                    $freezing_deposit = number_format($businessAccount['freezing_deposit'] - $taskCommod['price'] * $taskCommod['num'] * 1.05 - $taskCommod['freight_deposit'],2,'.','');     //冻结押金
                    $ava_deposit = number_format($businessAccount['ava_deposit'] + $taskCommod['price'] * $taskCommod['num'] * 1.05 + $taskCommod['freight_deposit'],2,'.','');               //可用押金
                    $ava_pepper = number_format($businessAccount['ava_pepper'] + $service_pepper,2,'.','');                                                                             //可用花椒
                    $freezing_golds = number_format($businessAccount['freezing_golds'] - $service_pepper,2,'.','');                                                                     //冻结花椒
                    //修改账号余额
                    $accout_arr = [
                        "ava_deposit" => $ava_deposit,
                        "ava_pepper" => $ava_pepper,
                        "freezing_deposit" => $freezing_deposit,
                        "freezing_golds" => $freezing_golds,
                        'bus_id' => $task_order['bus_id'],
                    ];
                    $account_res =$this->WBusinessAccountAccess->modifyAcount($accout_arr);
                    if(!$account_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],416,"买手取消任务回退商家资金失败");
                    }
                    //退押金记录
                    $deposit_arr = [
                        "bus_id" => $task_order['bus_id'],
                        "task_commod_id" => $taskCommod['id'],
                        "task_id" => $taskCommod['task_id'],
                        'task_order_id' => $task_order['id'],
                        'task_order_num' => $task_order['task_order_num'],
                        "expend" => 0,
                        "type" => 3,
                        "income" => number_format($taskCommod['price'] * $taskCommod['num']* 1.05 + $taskCommod['freight_deposit'],2,".",""),
                        "balance" => $ava_deposit,
                        "freeze_change" => -number_format($taskCommod['price'] * $taskCommod['num']* 1.05 + $taskCommod['freight_deposit'],2,".",""),
                        "freeze_balance" => $freezing_deposit,
                        "remark" => "买手撤销任务退还商家押金",
                        "operatorId" => $buyer['phone'],
                    ];
                    $deposite_res = $this->WBusDepositRecordAccess->addDepositRecord($deposit_arr);
                    if(!$deposite_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],417,"撤销任务退还押金失败");
                    }
                    //退花椒
                    $bus_pepper_arr = [
                        "bus_id" => $task_order['bus_id'],
                        "task_commod_id" => $task_order['task_commod_id'],
                        "task_id" => $taskCommod['task_id'],
                        'task_order_id' => $task_order['id'],
                        'task_order_num' => $task_order['task_order_num'],
                        "expend" => 0,
                        "type" => 3,
                        "income" => $service_pepper,
                        "balance" => $ava_pepper,
                        "freeze_change" =>-$service_pepper,
                        "freeze_balance" => $freezing_golds,
                        "remark" =>"撤销任务退还花椒",
                        "operatorId" => $buyer['phone']
                    ];
                    $bus_pepper_res = $this->WBusPepperRecordAccess->addBusPepperRecord($bus_pepper_arr);
                    if(!$bus_pepper_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],418,"买手撤销任务退还花椒失败");
                    }

                }else if($taskCommod['task_status'] == 4)
                {
                    $res_task = $this->WTaskCommodAccess->modifyTaskCommod(["task_answer" => $taskCommod['task_answer']-1,"surplus_task_num" => $taskCommod['surplus_task_num']+1,"id"=>$task_order['task_commod_id']]);
                    if(!$res_task)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],419,"取消任务失败");
                    }
                    //回退已经选择得关键字
                    if(($task_order['plat_type'] == 1 || $task_order['plat_type'] == 2) && ($task_order['task_type'] == 1 || $task_order['task_type'] == 2))
                    {
                        if(!empty($task_order['task_commod_keyword_id']))
                        {
                            //关键字查询
                            $task_commod_keyword = $this->RTaskCommodKeywordAccess->getTaskCommodKeywordById($task_order['task_commod_keyword_id']);
                            if(empty($task_commod_keyword))
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],420,"撤销任务回退关键字异常");
                            }
                            //回退个数
                            $res_task_commod_keyword = $this->WTaskCommodKeywordAccess->modifyTaskCommodKeyword(["use_num"=>$task_commod_keyword['use_num']-1,"task_commod_keyword_id"=> $task_commod_keyword['id']]);
                            if(!$res_task_commod_keyword)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],421,"撤销任务回退关键字失败");
                            }
                        }

                    }
                    //判断是否是取消图文任务
                    if(($task_order['plat_type'] == 1 || $task_order['plat_type'] == 2) && $task_order['task_type'] == 2)
                    {
                        if(!empty($task_order['task_shop_image_id']))
                        {
                            $task_shop_image = $this->RTaskShopImageAccess->getTaskShopDetail($task_order['task_shop_image_id']);
                            if(empty($task_shop_image))
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],422,"撤销任务回退图文任务异常");
                            }
                            $res_task_shop_image = $this->WTaskShopImageAccess->modifyTaskShop(['id' => $task_order['task_shop_image_id'],'is_effect'=>0]);
                            if(!$res_task_shop_image)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],423,"撤销图文任务回退失败");
                            }
                        }

                    }
                    if(($task_order['plat_type'] == 1 || $task_order['plat_type'] == 2) && ($task_order['task_type'] == 1 ||$task_order['task_type'] == 4 ))
                    {
                        //获取好评关键字id
                        if(!empty($task_order['praise_keywords_id']))
                        {
                            $praise_keyword = $this->RPraiseKeywordsAccess->getPraiseKeywordById($task_order['praise_keywords_id']);
                            if(empty($praise_keyword))
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],425,"撤销任务回退增值服务好评关键字异常");
                            }
                            $res_praise_keyword = $this->WPraiseKeywordsAccess->modifyPraisekeywords(['id' => $task_order['praise_keywords_id'],"is_user" =>1]);
                            if(!$res_praise_keyword)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],426,"撤销增值服务好评关键字失败");
                            }
                        }
                    }
                }

                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $data['buy_id'],
                    "task_status" => 10,
                    "status_remark" => "买手已放弃任务",
                    "remark" => '已放弃',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyer['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],427,"添加买手操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],428,"放弃任务失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],500,"操作异常".$e->getMessage());
        }
    }
    //确认收货
    public function confirmReceipt($data)
    {
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],401,"子订单id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],406,"买手账号不存在");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],407,"子任务单号不存在");
        }
        if($task_order['order_status'] != 5)
        {
            return Tool::returnJson([],408,"该订单不能确认收货");
        }
        //获取店铺信息
        $shop = $this->RShopAccess->getBusShopDetial($task_order['shop_id']);
        if(empty($shop))
        {
            return Tool::returnJson([],411,"商家店铺信息异常,请联系客服");
        }
        if($shop['is_print'] == 1)//打印 48小时后收货
        {
            //商家发货之后 48 小时 才可以收货
            if(time()-$task_order['merchant_time']<172800)
            {
                return Tool::returnJson([],410,"商家发货之后48小时才可以收货");
            }
        }else if($shop['is_print'] == 2)//不打印隔天收货
        {
            //商家发货之后 24 小时 才可以收货
            if(time()-$task_order['merchant_time']<86400)
            {
                return Tool::returnJson([],412,"商家发货之后24小时才可以收货");
            }
        }else if($shop['is_print'] == 3)//格4天收货
        {
            //商家发货之后 24 小时 才可以收货
            if(time()-$task_order['merchant_time']<345600)
            {
                return Tool::returnJson([],413,"商家发货之后96小时才可以收货");
            }
        }

        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->confirmTask(["order_status" => 6,"id" =>$data['task_order_id'] ]);
            if($res)
            {
                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $data['buy_id'],
                    "task_status" => 6,
                    "status_remark" => "已收货，待返款",
                    "remark" => '买手确认收货',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyer['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],425,"添加买手操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],409,"确认收货失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],500,"操作异常");
        }
    }
    //确认返款
    public function confirmRefunds($data)
    {
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],401,"子订单id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],406,"买手账号不存在");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],407,"子任务单号不存在");
        }
        if($task_order['order_status'] != 7)
        {
            return Tool::returnJson([],408,"不能进行该操作");
        }

        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            //判断返款方式 如果是平台返款  平台需要根据时间金额进行返款操作
            //商家返款 就直接改变状态就ok
            $res = $this->WTaskOrderAccess->confirmRefunds(["order_status" => 8,"id" =>$data['task_order_id'] ]);
            if($res)
            {
                $buy_account = $this->RBuyUserAcountAccess->getBuyUserAccount($data['buy_id']);
                $task_commod = $this->RTaskCommodAccess->getTaskDetail($task_order['task_commod_id']);
                if($task_order['return_type'] == 1)
                {
                    //增加买手本金记录
                    $deposit_arr = [
                        "buy_id" => $data['buy_id'],
                        "task_order_id" => $data['task_order_id'],
                        "task_id" => $task_order['task_id'],
                        "task_order_num" => $task_order['task_order_num'],
                        "task_commod_id" => $task_order['task_commod_id'],
                        "expend" => 0,
                        "type" =>3,
                        "income" => number_format($task_order['refund_amount'],2,".",""),
                        "balance" => number_format($task_order['refund_amount'] + $buy_account['remain_funds'],2,".",""),
                        "freeze_change"=> 0,
                        "freeze_balance"=> number_format($buy_account['freezing_funds']),
                        "remark" => "完成任务获得本金",
                        "operatorId" => $buyer['phone'],
                    ];
                    $deposit_res = $this->WDepositRecordAccess->addDepositRecord($deposit_arr);
                    if(!$deposit_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],409,"增加买手本金记录失败");
                    }
                    //增加买手佣金记录
                    $pepper_arr = [
                        "buy_id" => $data['buy_id'],
                        "task_id" => $task_order['task_id'],
                        "task_commod_id" => $task_order['task_commod_id'],
                        "task_order_num" => $task_order['task_order_num'],
                        'task_order_id' => $data['task_order_id'],
                        "expend" => 0,
                        "type" => 3,
                        "income" => number_format($task_order['commission'],2,".",""),
                        "balance" => number_format($task_order['commission'] + $buy_account['remail_golds'],2,".",""),
                        "freeze_change"=> 0,
                        "freeze_balance"=> number_format($buy_account['freezing_golds'],2,".",""),
                        "remark" => "完成任务获得花椒",
                        "operatorId" => $buyer['phone'],
                    ];
                    $pepper_res = $this->WPepperRecordAccess->addPepperRecord($pepper_arr);
                    if(!$pepper_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],410,"增加佣金记录异常");
                    }
                    //返还商家保证金 5%
                    $deposit = number_format($task_order['price'] * $task_order['num'] * 0.05,2,".","");
                    $bus_id = $task_order['bus_id'];
                    $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                    if(empty($business_account))
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],411,"商家账号异常");
                    }
                    $bus_deposite = [
                        "bus_id" => $task_order['bus_id'],
                        "task_commod_id" => $task_order['task_commod_id'],
                        "task_id" => $task_order['task_id'],
                        "task_order_id" => $task_order['id'],
                        "task_order_num" => $task_order['task_order_num'],
                        "expend" => 0,
                        'type' => 3,
                        "income" => $deposit,
                        "balance" =>number_format( $business_account['ava_deposit'] + $deposit,2,".",""),
                        "freeze_change"=> -$deposit,
                        "freeze_balance" => number_format($business_account['freezing_deposit'] - $deposit,2,".",""),
                        "remark" => "买手完成任务返还任务保证金",
                        "operatorId" => $buyer['phone'],
                    ];
                    $bus_deposit_res = $this->WBusDepositRecordAccess->addBusDepositRecord($bus_deposite);
                    if(!$bus_deposit_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],412,"买手完成任务返还任务保证金异常");
                    }
                    //修改商家账号信息
                    $bus_arr = [
                        "bus_id" => $task_order['bus_id'],
                        "total_deposit" => number_format($business_account['total_deposit'],2,".",""),
                        "ava_deposit" => number_format($business_account['ava_deposit'] + $deposit,2,".",""),
                        "freezing_deposit" => number_format($business_account['freezing_deposit'] - $deposit,2,".",""),
                        "freezing_golds" => number_format($business_account['freezing_golds'] - $task_order['commission'],2,".",""),
                    ];
                    $bus_res = $this->WBusinessAccountAccess->modifyBusinessAccount($bus_arr);
                    if(!$bus_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],413,"修改商家账号信息异常");
                    }
                    //运费返还
                    if($task_commod['freight_deposit'] >0)
                    {
                        $freight_deposit = number_format($task_commod['freight_deposit'],2,".","");
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],415,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 3,
                            "income" => $freight_deposit,
                            "balance" =>number_format( $business_account['ava_deposit'] + $freight_deposit,2,".",""),
                            "freeze_change"=> -$freight_deposit,
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $freight_deposit,2,".",""),
                            "remark" => "买手完成任务返还商家运费押金",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],416,"买手完成任务返还任务运费押金异常");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $freight_deposit,2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $freight_deposit,2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAccountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],417,"修改商家账号信息异常");
                        }

                    }

                    if((float)number_format($task_order['refund_amount']-$task_order['price']* $task_order['num'],2,".","") == 0)
                    {
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],415,"商家账号异常");
                        }
                        //增加本金记录
                        $bus_depsite_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 2,
                            "income" => 0,
                            "balance" =>number_format( $business_account['ava_deposit'],2,".",""),
                            "freeze_change"=> -$task_order['refund_amount'],
                            "freeze_balance" =>  number_format($business_account['freezing_deposit'] - $task_order['refund_amount'],2,".",""),
                            "remark" => "买手完成任务扣减商家押金给买手",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_deposite_ress = $this->WBusDepositRecordAccess->addDepositRecord($bus_depsite_arr);
                        if(!$bus_deposite_ress)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],4115,"买手完成任务扣减商家押金给买手失败");
                        }
                        //增加花椒记录
                        $bus_pepper_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 2,
                            "income" => 0,
                            "balance" =>number_format( $business_account['ava_pepper'],2,".",""),
                            "freeze_change"=> - number_format($task_commod['pay_task_commisse']/$task_commod['task_num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_golds']- ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                            "remark" => "买手完成任务扣减商家每单任务花椒",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_pepper_ress = $this->WBusPepperRecordAccess->addBusPepperRecord($bus_pepper_arr);
                        if(!$bus_pepper_ress)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],4225,"买手完成任务扣减商家每单任务花椒失败");
                        }

                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] - $task_order['refund_amount'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'],2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $task_order['refund_amount'],2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds']-($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                        ];
                        $bus_res = $this->WBusinessAccountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],438,"修改商家账号信息异常");
                        }
                    }
                    //修改商家扣减押金金额不一致情况  少扣押金的情况
                    if((float)number_format($task_order['refund_amount']-$task_order['price']* $task_order['num'],2,".","")>0)
                    {
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],415,"商家账号异常");
                        }
                        $ava_deposit = number_format( $business_account['ava_deposit'] -($task_order['refund_amount']-$task_order['price']* $task_order['num']),2,".","");
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => number_format($task_order['refund_amount']-$task_order['price']* $task_order['num'],2,".",""),
                            "income" => 0,
                            'type' => 2,
                            "balance" =>$ava_deposit,
                            "freeze_change"=> -number_format($task_order['price']* $task_order['num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $task_order['price']* $task_order['num'],2,".",""),
                            "remark" => "买手完成任务扣减押金不足的部分金额".($task_order['refund_amount']-$task_order['price']* $task_order['num']),
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],435,"增加商家任务返款押金不足部分金额扣减失败");
                        }
                        //增加花椒记录
                        $bus_pepper_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 2,
                            "income" => 0,
                            "balance" =>-$business_account['ava_pepper'],
                            "freeze_change"=> -number_format($task_commod['pay_task_commisse']/$task_commod['task_num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_golds']- ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                            "remark" => "买手完成任务扣减商家每单任务花椒",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_pepper_ress = $this->WBusPepperRecordAccess->addBusPepperRecord($bus_pepper_arr);
                        if(!$bus_pepper_ress)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],4225,"买手完成任务扣减商家每单任务花椒失败");
                        }

                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] - ($task_order['refund_amount']-$task_order['price']* $task_order['num']) - $task_order['price']* $task_order['num'],2,".",""),
                            "ava_deposit" => $ava_deposit,
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $task_order['price']* $task_order['num'],2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds']-($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                        ];
                        $bus_res = $this->WBusinessAccountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],436,"修改商家账号信息异常");
                        }
                    }
                    //修改商家扣减押金金额不一致情况  多扣押金的情况
                    if((float)number_format($task_order['refund_amount']-$task_order['price']* $task_order['num'],2,".","")< 0)
                    {
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],415,"商家账号异常");
                        }
                        $ava_deposit = number_format( $business_account['ava_deposit'] + ($task_order['price']* $task_order['num']- $task_order['refund_amount']),2,".","");
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 3,
                            "income" => number_format($task_order['price']* $task_order['num']- $task_order['refund_amount'],2,".",""),
                            "balance" => $ava_deposit,
                            "freeze_change"=> -number_format($task_order['price']* $task_order['num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - ($task_order['price']* $task_order['num']),2,".",""),
                            "remark" => "买手完成任务扣减押金多余的部分返还金额".($task_order['price']* $task_order['num']- $task_order['refund_amount']),
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],437,"增加商家任务返款押金多余部分金额返还失败");
                        }
                        //增加花椒记录
                        $bus_pepper_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 2,
                            "income" => 0,
                            "balance" =>-$business_account['ava_pepper'],
                            "freeze_change"=> -number_format($task_commod['pay_task_commisse']/$task_commod['task_num'],2,".",""),
                            "freeze_balance" => number_format($business_account['freezing_golds']- ($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                            "remark" => "买手完成任务扣减商家每单任务花椒",
                            "operatorId" => $buyer['phone'],
                        ];
                        $bus_pepper_ress = $this->WBusPepperRecordAccess->addBusPepperRecord($bus_pepper_arr);
                        if(!$bus_pepper_ress)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],4225,"买手完成任务扣减商家每单任务花椒失败");
                        }

                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] - $task_order['refund_amount'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + ($task_order['price']* $task_order['num'] - $task_order['refund_amount']),2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - ($task_order['price']* $task_order['num']),2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds']-($task_commod['pay_task_commisse']/$task_commod['task_num']),2,".",""),
                        ];
                        $bus_res = $this->WBusinessAccountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],438,"修改商家账号信息异常");
                        }
                    }

                    $buy_res = false;
                    //修改买手账号信息
                    if(empty($buy_account))
                    {
                        //增加买手账号记录
                        $buy_arr = [
                            "buy_id" => $data['buy_id'],
                            "remain_funds" => number_format($task_order['refund_amount'],2,".",""),
                            "remail_golds" => number_format($task_order['commission'],2,".",""),
                            "freezing_funds" => 0,
                            "freezing_golds" => 0,
                            "income_golds" => 0
                        ];
                        $buy_res = $this->WBuyUserAcountAccess->addUserAccount($buy_arr);
                    }else{
                        //修改买手账号记录
                        $buy_arr = [
                            "buy_id" => $data['buy_id'],
                            "remain_funds" => number_format($buy_account['remain_funds'] + $task_order['refund_amount'],2,".",""),
                            "remail_golds" => number_format($buy_account['remail_golds'] + $task_order['commission'],2,".",""),
                        ];
                        $buy_res = $this->WBuyUserAcountAccess->modifyUserAccount($buy_arr);
                    }
                    if(!$buy_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],415,"修改买手账号信息异常");
                    }
                }else if($task_order['return_type'] == 2){
                    //增加买手佣金记录
                    $pepper_arr = [
                        "buy_id" => $data['buy_id'],
                        'task_order_id' => $task_order['id'],
                        "task_order_num" => $task_order['task_order_num'],
                        'task_commod_id' => $task_order['task_commod_id'],
                        'task_id' => $task_order['task_id'],
                        "expend" => 0,
                        "type" => 3,
                        "income" => number_format($task_order['commission'],2,".",""),
                        "balance" => number_format($task_order['commission'] + $buy_account['remail_golds'],2,".",""),
                        "freeze_change" => 0,
                        "freeze_balance" => number_format($buy_account['freezing_golds'],2,".",""),
                        "remark" => "买手完成任务获得佣金花椒",
                        'operatorId' => $buyer['phone'],
                    ];
                    $pepper_res =$this->WPepperRecordAccess->addPepperRecord($pepper_arr);
                    if(!$pepper_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],416,"商家返款增加买手佣金记录异常");
                    }

                    //修改买手账号信息
                    $buy_res = false;
                    if(empty($buy_account))
                    {
                        //增加买手账号记录
                        $buy_arr = [
                            "buy_id" => $data['buy_id'],
                            "remain_funds" => 0,
                            "remail_golds" => number_format($task_order['commission'],2,".",""),
                            "freezing_funds" => 0,
                            "freezing_golds" => 0,
                            "income_golds" => number_format($task_order['commission'],2,".",""),
                        ];
                        $buy_res = $this->WBuyUserAcountAccess->addUserAccount($buy_arr);
                    }else
                    {
                        $buy_arr = [
                            "buy_id" => $data['buy_id'],
                            "remail_golds" =>number_format( $buy_account['remail_golds'] + $task_order['commission'],2,".",""),
                            "income_golds" => number_format($buy_account['income_golds'] + $task_order['commission'],2,".",""),
                        ];
                        $buy_res  = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($buy_arr);
                    }
                    if(!$buy_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],417,"修改买手账号信息异常");
                    }
                    //返还商家本金和 押金
                    //返还押金
                    //返还商家押金 5%
                    $deposit = number_format($task_order['price'] * $task_order['num'] * 0.05,2,".","");
                    if($deposit >0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],418,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 3,
                            "income" => -$deposit,
                            "balance" =>number_format( $business_account['ava_deposit'] + $deposit,2,".",""),
                            "freeze_change" => 0,
                            "freeze_balance" => number_format($business_account['freezing_deposit']- $deposit,2,".",""),
                            "remark" => "买手完成任务返还商家任务保证金",
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],419,"买手完成任务返还商家任务保证金异常");
                        }
                        $bus_deposite_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $deposit,2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit']- $deposit,2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'] - $task_order['commission'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAccountAccess->modifyBusinessAccount($bus_deposite_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],421,"修改商家账号信息异常");
                        }
                    }
                    //运费押金
                    //是否有运费押金 如果有退还商家运费押金
                    if($task_commod['freight_deposit']>0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],418,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            'type' => 3,
                            "income" => $task_commod['freight_deposit'],
                            "balance" =>number_format( $business_account['ava_deposit'] + $task_commod['freight_deposit'],2,".",""),
                            "freeze_change" => -$task_commod['freight_deposit'],
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $task_commod['freight_deposit'],2,".",""),
                            "remark" => "买手完成任务退还商家运费押金".($task_commod['freight_deposit']),
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],438,"增加商家任务返款押金不足部分金额扣减失败");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'],2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $task_commod['freight_deposit'],2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $task_commod['freight_deposit'],2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAccountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],439,"修改商家账号信息退还运费押金异常");
                        }
                    }
                    if($task_order['price'] * $task_order['num'] >0)
                    {
                        $bus_id = $task_order['bus_id'];
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($bus_id);
                        if(empty($business_account))
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],418,"商家账号异常");
                        }
                        $bus_deposite = [
                            "bus_id" => $task_order['bus_id'],
                            "task_commod_id" => $task_order['task_commod_id'],
                            "task_id" => $task_order['task_id'],
                            "task_order_id" => $task_order['id'],
                            "task_order_num" => $task_order['task_order_num'],
                            "expend" => 0,
                            "income" => number_format($task_order['price'] * $task_order['num'],2,".",""),
                            'type' => 2,
                            "balance" => number_format($business_account['ava_deposit'] + $task_order['price'] * $task_order['num'],2,".",""),
                            "freeze_change" => -(number_format($task_order['price'] * $task_order['num'],2,".","")),
                            "freeze_balance" => number_format($business_account['freezing_deposit'] - $task_order['price'] * $task_order['num'],2,".",""),
                            "remark" => "买手完成任务返还商家押金额-商家返款".number_format($task_order['price'] * $task_order['num'],2,".",""),
                            'operatorId' => $buyer['phone'],
                        ];
                        $bus_deposit_res = $this->WBusDepositRecordAccess->addBusDepositRecord($bus_deposite);
                        if(!$bus_deposit_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],435,"增加商家任务返款押金不足部分金额扣减失败");
                        }
                        //修改商家账号信息
                        $bus_arr = [
                            "bus_id" => $task_order['bus_id'],
                            "total_deposit" => number_format($business_account['total_deposit'] ,2,".",""),
                            "ava_deposit" => number_format($business_account['ava_deposit'] + $task_order['price'] * $task_order['num'],2,".",""),
                            "freezing_deposit" => number_format($business_account['freezing_deposit'] - $task_order['price'] * $task_order['num'],2,".",""),
                            "freezing_golds" => number_format($business_account['freezing_golds'],2,".",""),
                        ];
                        $bus_res = $this->WBusinessAccountAccess->modifyBusinessAccount($bus_arr);
                        if(!$bus_res)
                        {
                            $transaction->rollBack();
                            return Tool::returnJson([],421,"修改商家账号信息异常");
                        }
                    }
                }
                //增加买手完成任务获取花椒记录 任务花椒记录
                $task_pepper_array = [
                    "buy_id" => $data['buy_id'],
                    "tb_name" => $task_order['tbname'],
                    "task_order_id" => $task_order['id'],
                    "task_order_num" => $task_order['task_order_num'],
                    "pepper_income" => number_format($task_order['commission'],2,".",""),
                    "principal" => number_format($task_order['refund_amount'],2,".",""),
                    "remark" => "完成任务获得花椒"
                ];
                $task_peper_res = $this->WTaskPepperRecordAccess->addTaskPepperRecord($task_pepper_array);
                if(!$task_peper_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],422,"增加买手完成任务获取花椒记录异常");
                }
                //查询主订单下的子订单是否已经接完
                $task_orders_success_count = $this->RTaskOrderAccess->getTaskOrderCompleteCount($task_order['task_commod_id']);
                if($task_orders_success_count == $task_commod['task_num'])//判断所有的子任务是否已经完成
                {
                    $this->WTaskCommodAccess->taskCommodComplete(['id' => $task_order['task_commod_id'],"task_status" => 6]);
                }
                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $data['buy_id'],
                    "task_status" => 8,
                    "status_remark" => "已完成",
                    "remark" => '买手确认返款',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyer['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],425,"添加买手操作日志失败");
                }
                //增加邀请机制
                if($buyer['pid'] >0)
                {
                    $parentBuyUser = $this->RBuyUserAccess->getBuyUserInfo($buyer['pid']);
                    if($parentBuyUser['role'] ==1)//邀请人是买手
                    {
                        //邀请人返利
                        $parent_buyer_account = $this->RBuyUserAcountAccess->getBuyUserAccount($buyer['pid']);
                        //已经完成任务单数
                        $task_order_count = $this->RTaskOrderAccess->getBuyTaskOrderCount($buyer['id'],8);
//                        if($buyer['is_first'] ==1 && $task_order_count ==1)
                        if($task_order_count ==1)
                        {
                            $inviter_arr = [
                                "buy_id" => $buyer['pid'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 2,
                                "balance" => number_format($parent_buyer_account['remail_golds'] +2,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $parent_buyer_account['freezing_golds'],
                                "remark" => "邀请人-买手--买手首次完成首单任务获取2花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $buy_pepper_res = $this->WPepperRecordAccess->addPepperRecord($inviter_arr);
                            if(!$buy_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],427,"邀请人买手增加返利记录失败");
                            }
                            if(empty($parent_buyer_account))
                            {
                                $parent_arr = [
                                    "remain_funds" => 0,
                                    "remail_golds" => number_format(2,2,".",""),
                                    "freezing_funds" => 0,
                                    "freezing_golds" => 0,
                                    "income_golds" => number_format(2,2,".",""),
                                    "buy_id" => $buyer['pid'],
                                ];
                                $buy_users = $this->WBuyUserAcountAccess->addUserAccount($parent_arr);
                                if(!$buy_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }

                            }else
                            {
                                $parent_arr = [
                                    "remail_golds" => number_format($parent_buyer_account['remail_golds'] +2,2,".",""),
                                    "income_golds" => number_format($parent_buyer_account['income_golds'] +2,2,".",""),
                                    "buy_id" => $buyer['pid'],
                                ];
                                $parent_res = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($parent_arr);
                                if(!$parent_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }
                            //被邀请人账号信息
                            $invited_buy_account = $this->RBuyUserAcountAccess->getBuyUserAccount($data['buy_id']);
                            //被邀请人返利
                            $invited_arr = [
                                "buy_id" => $data['buy_id'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 3,
                                "balance" => number_format($invited_buy_account['remail_golds'] +3,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $invited_buy_account['freezing_golds'],
                                "remark" => "被邀请人-买手-首次完成首单任务获取3花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $invited_buy_pepper_res = $this->WPepperRecordAccess->addPepperRecord($invited_arr);
                            if(!$invited_buy_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],429,"增加被邀请人返利失败");
                            }

                            if(empty($invited_buy_account))
                            {
                                $parent_arr = [
                                    "remain_funds" => 0,
                                    "remail_golds" =>3,
                                    "freezing_funds" => 0,
                                    "freezing_golds" => 0,
                                    "income_golds" => 3,
                                    "buy_id" => $data['buy_id'],
                                ];
                                $buy_users = $this->WBuyUserAcountAccess->addUserAccount($parent_arr);
                                if(!$buy_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }else
                            {
                                $invited_arr = [
                                    "remail_golds" => number_format($invited_buy_account['remail_golds'] + 3,2,".",""),
                                    "income_golds" => number_format($invited_buy_account['income_golds'] + 3,2,".",""),
                                    "buy_id" => $data['buy_id'],
                                ];
                                $invited_buy_res = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($invited_arr);
                                if(!$invited_buy_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],430,"被邀请人返利失败");
                                }
                            }
                        }else
                        {
                            $inviter_arr = [
                                "buy_id" => $buyer['pid'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 1,
                                "balance" => number_format($parent_buyer_account['remail_golds'] +1,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $parent_buyer_account['freezing_golds'],
                                "remark" => "被邀请人完成任务-邀请人买手获取1花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $buy_pepper_res = $this->WPepperRecordAccess->addPepperRecord($inviter_arr);
                            if(!$buy_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],431,"邀请人买手增加返利记录失败");
                            }
                            if(empty($parent_buyer_account))
                            {
                                $parent_arr = [
                                    "remain_funds" => 0,
                                    "remail_golds" =>1,
                                    "freezing_funds" => 0,
                                    "freezing_golds" => 0,
                                    "income_golds" => 1,
                                    "buy_id" => $buyer['pid'],
                                ];
                                $buy_users = $this->WBuyUserAcountAccess->addUserAccount($parent_arr);
                                if(!$buy_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }else
                            {
                                $parent_arr = [
                                    "remail_golds" => number_format($parent_buyer_account['remail_golds'] +1,2,".",""),
                                    "income_golds" => number_format($parent_buyer_account['income_golds'] +1,2,".",""),
                                    "buy_id" => $buyer['pid'],
                                ];
                                $parent_res = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($parent_arr);
                                if(!$parent_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],432,"修改邀请人返利失败");
                                }
                            }
                        }
                    }else if($parentBuyUser['role'] ==2)//邀请人是商家
                    {
                        //邀请人是商家的情况  商家邀请买手  返利给商家  返利给买手
                        $business_account = $this->RBusinessAccountAccess->getBusinessAccount($buyer['pid']);
                        //已经完成任务单数
                        $task_order_count = $this->RTaskOrderAccess->getBuyTaskOrderCount($buyer['id'],8);
//                        if($buyer['is_first'] ==1 && $task_order_count ==1)
                        if($task_order_count ==1)
                        {
                            $inviter_arr = [
                                "bus_id" => $buyer['pid'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 2,
                                "balance" => number_format($business_account['ava_pepper'] + 2,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $business_account['freezing_golds'],
                                "remark" => "买手首次邀请首次完成任务-邀请人-商家获取2花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $business_pepper_res = $this->WBusPepperRecordAccess->addBusPepperRecord($inviter_arr);
                            if(!$business_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],427,"邀请人商家增加返利记录失败");
                            }
                            if(empty($business_account))
                            {
                                $parent_arr = [
                                    "total_deposit" => 0,
                                    "ava_deposit" =>0,
                                    "freezing_deposit" => 0,
                                    "ava_pepper" =>2,
                                    "freezing_golds" => 0,
                                    "cash_deposit" => 0,
                                    "bus_id" => $buyer['pid'],
                                ];
                                $business_users = $this->WBusinessAccountAccess->addBusinessAccount($parent_arr);
                                if(!$business_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }

                            }else
                            {
                                $parent_arr = [
                                    "freezing_deposit" => $business_account['freezing_deposit'],
                                    "ava_deposit" => $business_account['ava_deposit'],
                                    "ava_pepper" => number_format($business_account['ava_pepper'] +2,2,".",""),
                                    "freezing_golds" =>$business_account['freezing_golds'],
                                    "bus_id" => $buyer['pid'],
                                ];
                                $parent_res = $this->WBusinessAccountAccess->modifyAcount($parent_arr);
                                if(!$parent_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }

                            //被邀请人返利
                            $invited_buy_account = $this->RBuyUserAcountAccess->getBuyUserAccount($data['buy_id']);
                            $invited_arr = [
                                "buy_id" => $data['buy_id'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 3,
                                "balance" => number_format($invited_buy_account['remail_golds'] +3,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $invited_buy_account['freezing_golds'],
                                "remark" => "被邀请人-买手-首次邀请首次完成任务-获取3花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $invited_buy_pepper_res = $this->WPepperRecordAccess->addPepperRecord($invited_arr);
                            if(!$invited_buy_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],429,"增加被邀请人返利失败");
                            }

                            if(empty($invited_buy_account))
                            {
                                $parent_arr = [
                                    "remain_funds" => 0,
                                    "remail_golds" =>3,
                                    "freezing_funds" => 0,
                                    "freezing_golds" => 0,
                                    "income_golds" => 3,
                                    "buy_id" => $data['buy_id'],
                                ];
                                $buy_users = $this->WBuyUserAcountAccess->addUserAccount($parent_arr);
                                if(!$buy_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }else
                            {
                                $invited_arr = [
                                    "remail_golds" => number_format($invited_buy_account['remail_golds'] + 3,2,".",""),
                                    "income_golds" => number_format($invited_buy_account['income_golds'] + 3,2,".",""),
                                    "buy_id" => $data['buy_id'],
                                ];
                                $invited_buy_res = $this->WBuyUserAcountAccess->modifyIncomeUserAccount($invited_arr);
                                if(!$invited_buy_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],430,"被邀请人返利失败");
                                }
                            }

                        }else//非首单
                        {
                            $inviter_arr = [
                                "bus_id" => $buyer['pid'],
                                "task_order_num" => $task_order['task_order_num'],
                                "task_order_id" => $task_order['id'],
                                "type" => 7,
                                "task_id" => $task_order['task_id'],
                                "task_commod_id" => $task_order['task_commod_id'],
                                "expend" => 0,
                                "income" => 1,
                                "balance" => number_format( $business_account['ava_pepper'] + 1,2,".",""),
                                "freeze_change" => 0,
                                "freeze_balance" => $business_account['freezing_golds'],
                                "remark" => "邀请人-商家-买手完成任务-非首次邀请非首单-获取1花椒",
                                "operatorId" => $buyer['phone'],
                            ];
                            $business_pepper_res = $this->WBusPepperRecordAccess->addBusPepperRecord($inviter_arr);
                            if(!$business_pepper_res)
                            {
                                $transaction->rollBack();
                                return Tool::returnJson([],427,"邀请人商家增加返利记录失败");
                            }
                            if(empty($business_account))
                            {
                                $parent_arr = [
                                    "total_deposit" => 0,
                                    "ava_deposit" =>0,
                                    "freezing_deposit" => 0,
                                    "ava_pepper" =>1,
                                    "freezing_golds" => 0,
                                    "cash_deposit" => 0,
                                    "bus_id" => $buyer['pid'],
                                ];
                                $business_users = $this->WBusinessAccountAccess->addBusinessAccount($parent_arr);
                                if(!$business_users)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }

                            }else
                            {
                                $parent_arr = [
                                    "freezing_deposit" => $business_account['freezing_deposit'],
                                    "ava_deposit" => $business_account['ava_deposit'],
                                    "ava_pepper" => number_format($business_account['ava_pepper'] +1,2,".",""),
                                    "freezing_golds" =>$business_account['freezing_golds'],
                                    "bus_id" => $buyer['pid'],
                                ];
                                $parent_res = $this->WBusinessAccountAccess->modifyAcount($parent_arr);
                                if(!$parent_res)
                                {
                                    $transaction->rollBack();
                                    return Tool::returnJson([],428,"邀请人返利失败");
                                }
                            }
                        }
                    }
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],409,"确认返款失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],500,"操作异常".$e->getMessage());
        }
    }
    //驳回返款
    public function rejectRefunds($data)
    {
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],401,"子订单id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }
        if(empty($data['actual_refunds']))
        {
            return Tool::returnJson([],403,"审核金额不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],405,"安全码不能为空");
        }
        if(md5($data['task_order_id'].$data['buy_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],406,"安全码不正确");
        }
        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],407,"买手账号不存在");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],408,"子任务单号不存在");
        }
        if(!($task_order['order_status'] == 7 || $task_order['order_status'] == 12))
        {
            return Tool::returnJson([],409,"不能进行该操作");
        }
        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->rejectRefunds(["order_status" => 9,"id" =>$data['task_order_id'],'actual_refunds' => $data['actual_refunds'] ]);
            if($res)
            {
                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $data['buy_id'],
                    "task_status" => 9,
                    "status_remark" => "已驳回",
                    "remark" => '买手已驳回',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyer['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],425,"添加买手操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],410,"驳回返款失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],500,"操作异常");
        }
    }
    //上传好评图片
    public function uploadPositive($data)
    {
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],401,"子订单id不能为空");
        }
        if(empty($data['buy_id']))
        {
            return Tool::returnJson([],402,"买手id不能为空");
        }
        if(empty($data['parise_url']))
        {
            return Tool::returnJson([],403,"好评图片不能为空");
        }
        if(empty($data['tao_parise_url']))
        {
            return Tool::returnJson([],405,"淘宝好评图片不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],406,"安全码不能为空");
        }
        if(md5($data['buy_id'].$data['task_order_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],407,"安全码不正确");
        }

        $buyer = $this->RBuyUserAccess->getBuyUserInfo($data['buy_id']);
        if(empty($buyer))
        {
            return Tool::returnJson([],408,"买手账号不存在");
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderById($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],409,"子任务单号不存在");
        }
        if($task_order['order_status'] != 5)
        {
            return Tool::returnJson([],410,"不能进行该操作");
        }
        //开启事务
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->uploadPositive(["parise_url" => $data['parise_url'],"id" =>$data['task_order_id'],'tao_parise_url' => $data['tao_parise_url'],"order_status" => 6]);
            if($res)
            {
                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $data['buy_id'],
                    "task_status" => 6,
                    "status_remark" => "已收货待返款",
                    "remark" => '买手上传好评截图确认收货',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyer['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],425,"添加买手操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],411,"上传好评图片失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],500,"操作异常".$e->getMessage());
        }

    }
}